package com.spring.sunflower.conversation

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.CallRecordBean

/**
 * Created by Kevin on 2021/9/1<br/>
 *
 * Describe:<br/>
 */
class CallRecordActivity : BaseActivity<CallRecordPresenter>(),
    ConversationContract.CallRecordView, OnLoadMoreListener, OnRefreshListener {
    private var refreshLayout: SmartRefreshLayout? = null
    private var rvRecyclerView: RecyclerView? = null
    private var mAdapter: CallRecordAdapter? = null
    private var mData: MutableList<CallRecordBean.DatasBean> = ArrayList()
    private var pageIndex = 1
    private var llEmpty: LinearLayout? = null
    override fun getLayoutID(): Int = R.layout.activity_bill_detail

    override fun setPresenter(): CallRecordPresenter = CallRecordPresenter(this)

    override fun initView() {
        tvTitle.text = "通话记录"
        llEmpty = findViewById(R.id.llEmpty)
        refreshLayout = findViewById(R.id.refreshLayout)
        rvRecyclerView = findViewById(R.id.rvRecyclerView)
        mAdapter = CallRecordAdapter(R.layout.adapter_item_call_record, mData)
        val linearLayoutManager = LinearLayoutManager(this)
        rvRecyclerView!!.layoutManager = linearLayoutManager
        rvRecyclerView!!.adapter = mAdapter
        refreshLayout!!.setOnRefreshListener(this)
        refreshLayout!!.setOnLoadMoreListener(this)
        mPresenter.getCallRecord(pageIndex.toString())
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPresenter.getCallRecord((++pageIndex).toString())

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageIndex = 1
        mPresenter.getCallRecord(pageIndex.toString())

    }

    override fun showCallRecord(data: List<CallRecordBean.DatasBean>) {
        if (pageIndex == 1) {
            if (data.isNullOrEmpty()) {
                llEmpty!!.visibility = View.VISIBLE
                refreshLayout!!.visibility = View.GONE
            } else {
                llEmpty!!.visibility = View.GONE
                refreshLayout!!.visibility = View.VISIBLE
                mAdapter!!.setList(data)
            }
        } else {
            mAdapter!!.addData(data!!)
        }
        if (refreshLayout!!.isLoading) {
            refreshLayout!!.finishLoadMore()
        }
        if (refreshLayout!!.isRefreshing) {
            refreshLayout!!.finishRefresh()
        }
    }
}